/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jobportal.company;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.text.SimpleDateFormat;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;
import jobportal.Dbconn;


/**
 *
 * @author project
 */
public class EditPostedJob extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        RequestDispatcher rd;
        Connection con;
        Statement st;
        ResultSet rs;
        String error = "";
        boolean flag = true;
        String compid, venue, jobtitle, metakeys, jobdesc, desired_profile, walkindate, validuntil, gender, nationality, emp_type, careerlevel, jobrole, category, qual, minexp, maxexp, salary, current_sal_per, keyskill, joblocation, contname, contphone, contemail,status="Not Verified", sql;
        PrintWriter out = response.getWriter();
        try {
            con = Dbconn.getConn();
            st = con.createStatement();
            HttpSession session = request.getSession();
            jobtitle = request.getParameter("jobtitle");
            metakeys = request.getParameter("metakeys");
            jobdesc = request.getParameter("jobdesc");
            desired_profile = request.getParameter("desired_profile");
            walkindate = request.getParameter("walkindate");
            java.util.Date dt = new SimpleDateFormat("dd-MMM-yy").parse(request.getParameter("walkindate"));
            SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
            walkindate = s.format(dt);
            validuntil = request.getParameter("validuntil");
            dt = new SimpleDateFormat("dd-MMM-yy").parse(request.getParameter("validuntil"));
            s = new SimpleDateFormat("yyyy-MM-dd");
            validuntil = s.format(dt);
            gender = request.getParameter("gender");
            nationality = request.getParameter("nationality");
            emp_type = request.getParameter("emp_type");
            careerlevel = request.getParameter("careerlevel");
            jobrole = request.getParameter("jobrole");
            category = request.getParameter("category");
            qual = request.getParameter("qual");
            minexp = request.getParameter("minexp");
            maxexp = request.getParameter("maxexp");
            salary = request.getParameter("salary");
            current_sal_per = request.getParameter("current_sal_per");
            keyskill = request.getParameter("keyskill");
            joblocation = request.getParameter("joblocation");
            contname = request.getParameter("contname");
            contphone = request.getParameter("contphone");
            contemail = request.getParameter("contemail");
            venue = request.getParameter("venue");
            sql = "update jobs set jobtitle=?,metakeys=?,jobdesc=?,desiredprof=?,walkindate=?,validuntil=?,gender=?,nationality=?,emptype=?,careerlevel=?,jobrole=?,funarea=?,qualification=?,minexp=?,maxexp=?,salary=?,salarytype=?,keyskill=?,joblocation=?,contname=?,contphone=?,contemail=?,venue=?,jobpostdate=now(),status='"+status+"' where compid='" + session.getAttribute("username") + "' ";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, jobtitle);
            ps.setString(2, metakeys);
            ps.setString(3, jobdesc);
            ps.setString(4, desired_profile);
            ps.setString(5, walkindate);
            ps.setString(6, validuntil);
            ps.setString(7, gender);
            ps.setString(8, nationality);
            ps.setString(9, emp_type);
            ps.setString(10, careerlevel);
            ps.setString(11, jobrole);
            ps.setString(12, category);
            ps.setString(13, qual);
            ps.setString(14, minexp);
            ps.setString(15, maxexp);
            ps.setString(16, salary);
            ps.setString(17, current_sal_per);
            ps.setString(18, keyskill);
            ps.setString(19, joblocation);
            ps.setString(20, contname);
            ps.setString(21, contphone);
            ps.setString(22, contemail);
            ps.setString(23, venue);
            if (ps.executeUpdate() > 0) {
                rd = request.getRequestDispatcher("post_jobs_edit.jsp");
                rd.forward(request, response);
            } else {
                rd = request.getRequestDispatcher("post_jobs_edit.jsp?status=" + error);
                rd.forward(request, response);
            }

        } catch (Exception e) {
            flag = false;
            error += " Exception=" + e;
            out.println("Exception::" + e);

            //JOptionPane.showMessageDialog(null, "Excepction occurred" + e, "Messging Error:", JOptionPane.WARNING_MESSAGE);
        }

    }
}
